﻿using System;
using System.Collections.Generic;

namespace SeaBattle.Domain.Entities
{
    public class Game
    {
        public Game(int id, DateTime created, Player creator)
        {
            Id = id;
            Created = created;
            Creator = creator;
            State = GameState.STATE_CREATED;
            
        }

        private void InitializeFleet(Player player)
        {

        }

        private Player opponent;

        public int Id { get; private set; }
        public int Preset { get; set; }
        /// <summary>
        /// Gets the created.
        /// </summary>
        public DateTime Created { get; private set; }
        /// <summary>
        /// Gets the creator.
        /// </summary>
        public Player Creator { get; private set; }
        /// <summary>
        /// Gets or sets the opponent.
        /// </summary>
        /// <value>
        /// The opponent.
        /// </value>
        public Player Opponent { get { return opponent; } set{SetOpponent(value);} }
        public GameState State { get; private set; }

        private void SetOpponent(Player theOpponent)
        {
            // The game must be in Opened status
            if(!Opened)
            {
                throw new Exception("The game "+Id+" not opened");
            }
            // Check whether Creator and Opponent are not the same persons
            if(Creator == theOpponent)
            {
                throw new Exception("The opponent is the same as creator of the game "+Id);
            }
            // Set opponent
            opponent = theOpponent;
        }

        public void JoinOpponent(Player theOpponent)
        {
            Opponent = theOpponent;
            BoardPreparing = true;            
        }

        public bool Opened
        {
            get { return State == GameState.STATE_OPENED; }
            set
            {            
                // Check 
                // Nop
                // Change real state
                if (value)
                {
                    State = GameState.STATE_OPENED;
                }
            }
        }
        public bool Closed
        {
            get { return State == GameState.STATE_CLOSED; }
            set
            {
                // Check 
                // Nop
                // Change real state
                if (value)
                {
                    State = GameState.STATE_CLOSED;
                }
            }
        }
        public bool BoardPreparing
        {
            get { return State == GameState.STATE_BOARD_SETUP; }
            set
            {
                // Check 
                // Nop
                // Change real state
                if (value)
                {
                    State = GameState.STATE_BOARD_SETUP;
                }
            }
        }

        public Dictionary<Player, Fleet> Navies { get; set; }

    }

    public enum GameState
    {        
        STATE_CREATED = 0,
        STATE_GAME_SETUP,
        STATE_OPENED,
        STATE_BOARD_SETUP, // Setup game board
        STATE_PLAY,
        STATE_PAUSE,
        STATE_CLOSED
    }
}
